Home:ALL Converter>Type conversion using TypeScript interface

Type conversion using TypeScript interface

Ask Time:2020-02-24T01:05:26         Author:Rcls

Json Formatter

This is kind of a weird question but I'm working on a massive form containing approximately ~100 input fields. We're passing all this data to a GraphQL endpoint. We're using TypeScript and I've defined Interfaces based on the schema.

I want to know if I can use TypeScript's interface types, to be specific the type boolean, to convert certain property values automatically to true or false since they arrive from the form as strings "1" or "0", or "true" (checkboxes)? I could then build something of a filter/converter to convert certain input types for GraphQL requests automatically when I can detect it from the Interface. There was a suggestion to do something like this, here on StackOverflow:

interface I1 {
    x: boolean; 
}

let myVar: I1['x'];

But you cannot use myVar as it is defined. I would like to get typeof I1.x so I can get back 'boolean' and then do a conversion for some fields such as !!(obj.x) etc.

So can I go about this or do I really have to generate a massive object tree converting or defining specific keys, again (duplication), to be boolean? Or do we have to think about using boolean values with GraphQ?

Author:Rcls,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/60364676/type-conversion-using-typescript-interface
yy